package com.jml.整合springboot.service;

import com.alibaba.fastjson.JSONObject;
import java.util.UUID;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class FanoutProducer全局唯一ID {
	@Autowired
	private AmqpTemplate amqpTemplate;
	public void send(String queueName) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("email", "123@qq.com");
		jsonObject.put("timestamp", System.currentTimeMillis());
		String jsonString = jsonObject.toJSONString();
		System.out.println("jsonString:" + jsonString);
		//放入全局ID，这里用的是UUID
		MessageBuilder.withBody(jsonString.getBytes())
				.setContentType(MessageProperties.CONTENT_TYPE_JSON)
				.setContentEncoding("utf-8")
				.setMessageId(UUID.randomUUID() + "")
				.build();

		amqpTemplate.convertAndSend(queueName, jsonString);
	}
}

